import React, { useEffect, useState, useRef } from 'react'

function App () {
  const [count, setCount] = useState(0);
  let timerId = useRef()
  useEffect(() => {
    timerId.current = setInterval(() => {
      setCount(count => count + 1)
    }, 1000)
  }, [])

  const stopCount = () => {
    clearInterval(timerId.current)
  }
  return (
    <div>
    {count}
    <button onClick={stopCount}>停止</button>
  </div>
  )
}

export default App;
